package com.honeywen.mymvc.leetcode;

import java.util.Collections;

/**
 * 121. 买卖股票的最佳时机
 * https://leetcode-cn.com/problems/best-time-to-buy-and-sell-stock/description/
 *
 * @author wangwei
 * @date 2018/12/13 下午3:18
 */
public class StockTest {

    public static void main(String[] args) {
        System.out.println(maxProfit2(new int[]{7, 1, 5, 3, 6, 4}));
        System.out.println(maxProfit2(new int[]{7, 6, 4, 3, 1}));
        System.out.println(maxProfit2(new int[]{2, 1, 4}));

    }

    public static int maxProfit2(int[] prices) {

        int low = 0, high = 0, max = 0, tmp = 0;

        for (int i = 0; i < prices.length; i++) {

            high = i;
            tmp = prices[high] - prices[low];
            if (tmp <= 0) {
                low = i;
            }
            if (tmp > max) {
                max = tmp;
            }

        }
        return max;

    }

    public static int maxProfit(int[] prices) {

        if (prices == null || prices.length < 2) {
            return 0;
        }

        int len = prices.length;

        int max = prices[len - 1] - prices[0];
        for (int i = 0; i < len - 1; i++) {
            for (int j = i + 1; j < len; j++) {

                if (prices[j] - prices[i] > max) {
                    max = prices[j] - prices[i];
                }
            }
        }


        return max < 0 ? 0 : max;

    }
}
